#include "passivehandler.h"

PassiveHandler::PassiveHandler(QObject *parent) : NetWorkHandler(parent),
    m_graberClass(Q_NULLPTR),
    m_inputSimulator(Q_NULLPTR),
    m_vpxEncoder(new VpxEncoder(this))
{
    connect(m_vpxEncoder,&VpxEncoder::sendEncodeImageData,this,&PassiveHandler::sendVpxImage);
}
void PassiveHandler::imageEnqueue(int pox,int poy,const QImage &image,int sendNum){
    m_vpxEncoder->putToEncoder(pox,poy,image,sendNum);
}

void PassiveHandler::sendHearbeat(){
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeHeartbeat);
    ex.set_resourceid(this->m_transferId.toStdString());
    serializeSend(ex);
}
void PassiveHandler::sendImageParameters(const QSize &imageSize){
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeImageParameters);
    ex.set_resourceid( this->m_transferId.toStdString());
    BigPack::ScImageParameters *para = new BigPack::ScImageParameters();
    para->set_imagewidth(imageSize.width());
    para->set_imageheight(imageSize.height());
    ex.set_allocated_imageparameters(para);

    serializeSend(ex);
}

void PassiveHandler::sendVpxImage(quint16 posX, quint16 posY,int width,int height,
                                   const QByteArray &imageData, quint16 tileNum){
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeImage);
    ex.set_resourceid( this->m_transferId.toStdString());
    BigPack::ScImageInfo *info = new BigPack::ScImageInfo();
    info->set_posx(static_cast<qint32>(posX));
    info->set_posy(static_cast<qint32>(posY));
    info->set_imagewidth(width);
    info->set_imageheight(height);
    info->set_m_data(imageData,imageData.size());
    info->set_sendnum(static_cast<qint32>(tileNum));
    ex.set_allocated_image(info) ;

    serializeSend(ex);
}
void PassiveHandler::dealProto(int i,BigPack::Exchange resv_exc){
    switch(i){
    case BigPack::Exchange::TypeReplyRegisterDetails:
    {
        if(resv_exc.replyinfo().success()){
            this->m_transferId = QString::fromStdString(resv_exc.replyinfo().registerid());
            emit showDeviceId(this->m_transferId);
        }
        break;
    }
    case BigPack::Exchange::TypeRequestAuth:
    {
        qDebug()<<"进入请求认证协议";
        QString password = QString::fromStdString(resv_exc.requestauth().targetpasswd());
        BigPack::ScResponseAuth *responseauth = new BigPack::ScResponseAuth();
        if(password == m_ntInfo->getTempPass()){
            responseauth->set_success(true);
        }else{
            responseauth->set_success(false);
        }
        BigPack::Exchange write_ex ;
        write_ex.set_datatype(BigPack::Exchange::TypeResponseAuth);
        write_ex.set_resourceid(resv_exc.targetid());
        write_ex.set_targetid(resv_exc.resourceid());
        write_ex.set_allocated_responseauth(responseauth);
        std::string s_info ;
        write_ex.SerializeToString(&s_info);
        m_tcpSocket->write(intToBytes(write_ex.ByteSizeLong()),sizeof(qint32));
        m_tcpSocket->write(s_info.c_str(),write_ex.ByteSizeLong());
        m_tcpSocket->flush();
        break;
    }
    case BigPack::Exchange::TypeRequestDesk:
    {
        if(1 == resv_exc.requestdesk().operation()){
            emit startSreenShot() ;
        }else if(0 == resv_exc.requestdesk().operation()){
            emit closeSreenShot();
        }else if(2 == resv_exc.requestdesk().operation()){
            emit newConnectSoFlash();
        }
        break;
    }
    case BigPack::Exchange::TypeMouseMove:{
        emit setMouseMove(resv_exc.mousemove().posx(),resv_exc.mousemove().posy());
        break;
    }
    case BigPack::Exchange::TypeMouseKeys:{
        emit setMousePressed(resv_exc.mousekeys().keycode(),resv_exc.mousekeys().state());
        break;
    }
    case  BigPack::Exchange::TypeWheelEvent:{
        emit setWheelChanged(resv_exc.wheelevent().deltapos());
        break;
    }
    case BigPack::Exchange::TypeKeyBoard:{
        emit setKeyPressed(resv_exc.keyboard().keycode(),resv_exc.keyboard().state());
        break;
    }
    case BigPack::Exchange::TypeImageReceived:{
        emit receivedNum(resv_exc.imagereceived().tilenum());
        break;
    }
    }
}
void PassiveHandler::createToHandler(){
    //数据处理
    //抓屏幕
    m_graberClass = new GraberClass(this);
    //输入模拟
    m_inputSimulator = new InputSimulator(this) ;
    //启动抓屏幕（与心跳）
    m_graberClass->start();
    //连接成功，发送显示ID
    connect(this,SIGNAL(showDeviceId(QString)),this,SIGNAL(tfShowDeviceId(QString)));
    //图像
    connect(m_graberClass, &GraberClass::imageParameters,this, &PassiveHandler::sendImageParameters);
    connect(m_graberClass, &GraberClass::originalImage,this, &PassiveHandler::imageEnqueue);
    connect(m_graberClass, &GraberClass::screenPositionChanged, m_inputSimulator, &InputSimulator::setScreenPosition);
    //心跳
    connect(m_graberClass, &GraberClass::heartBeatTimeOut, this, &PassiveHandler::sendHearbeat);
    //控制显示
    connect(this, &PassiveHandler::startSreenShot, m_graberClass, &GraberClass::startSending);
    connect(this, &PassiveHandler::receivedNum, m_graberClass, &GraberClass::setReceivedNum);
    connect(this, &PassiveHandler::closeSreenShot, m_graberClass, &GraberClass::stopSending);
    connect(this, &PassiveHandler::newConnectSoFlash, m_graberClass, &GraberClass::newSoFlash);
    connect(this, &PassiveHandler::changeDisplayNum, m_graberClass, &GraberClass::changeScreenNum);
    //输入
    connect(this, &PassiveHandler::setKeyPressed, m_inputSimulator, &InputSimulator::simulateKeyboard);
    connect(this, &PassiveHandler::setMousePressed, m_inputSimulator, &InputSimulator::simulateMouseKeys);
    connect(this, &PassiveHandler::setMouseMove, m_inputSimulator, &InputSimulator::simulateMouseMove);
    connect(this, &PassiveHandler::setWheelChanged, m_inputSimulator, &InputSimulator::simulateWheelEvent);
    connect(this, &PassiveHandler::setMouseDelta, m_inputSimulator, &InputSimulator::setMouseDelta);

}
